//
//  ZPLoginModule.m
//  ZPIOSEducation
//
//  Created by mini1 on 2016/12/21.
//  Copyright © 2016年 mini1. All rights reserved.
//

#import "ZPLoginModule.h"
#import "ZPAuthStore.h"
//等于废弃
static ZPLoginModule *_loginManager = nil;

@implementation ZPLoginModule

ZPSingletonM(ZPLoginModule)

- (void)loginWithUserName:(NSString *)userName password:(NSString *)password success:(void(^)(NSString *successMessage))success failure:(void(^)(NSString *errorMeaasge))failure
{
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSDictionary *paramDict = @{@"mobile":userName,
                                @"password":[password zp_md5String],
                                @"idfv":@"0"
                                };
    
    [manager POST:APIPPT@"access/onLogin.json" parameters:paramDict progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
         ZPLog(@"responseObject == %@",responseObject);
         if ([[responseObject  objectForKey:@"code"] integerValue] == 200)
         {
             NSDictionary  *resultDic = [responseObject objectForKey:@"result"];
             if ([resultDic  isKindOfClass:[NSDictionary  class]]) {
                 NSMutableDictionary  *tempDic = [NSMutableDictionary  dictionaryWithDictionary:resultDic];
                 [tempDic setObject:userName forKey:@"userName"];
                 [tempDic setObject:password forKey:@"passwd"];
                 [[ZPAuthStore sharedZPAuthStore] dicToUserEntity:tempDic]; // 服务器返回数据写入实体文件
                 [[ZPAuthStore sharedZPAuthStore] resetUserInfoDictionary];
                 [[ZPAuthStore sharedZPAuthStore] setWebViewCookiesWithType:1];
                 NSString *ids = [NSMutableDictionary changeType:[tempDic objectForKey:@"ids"]];
                 [self getStudentById:ids];
                 [zUserDefaults setObject:[NSString stringWithFormat:@"%@",userName] forKey:LastLogInfomation];
                 success(@"登录成功");
                 
             }else {
                 failure(@"登录失败");
             }
             
         }
         else
         {
             failure(responseObject[@"codeMsg"]);
         }
     }
          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
              if (!((ZPAppDelegate *)[[UIApplication sharedApplication] delegate]).isHasNetwork || error.code==-1004 || error.code==-1003) {
                  failure(@"请检查您的网络");
              }
              else
              {
                  failure(@"登录失败");
              }
          }];
}

//////////////////////////////////////////////////////////////////////////////
-(void)getStudentById:(NSString *)sid{
    [[ZPNetworking sharedZPNetworking] GET:APIUC@"uc/user/getStudentById.json" parameters:@{@"id":sid} progress:nil success:^(id  _Nonnull responseObject) {
        if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
            NSLog(@"responseObject=responseObject=====%@",responseObject);
            NSDictionary  *resultDic = [responseObject  objectForKey:@"result"];
            
            NSString *homePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/UserInfo.plist"];
            NSMutableDictionary *dataDic = [[NSDictionary dictionaryWithContentsOfFile:homePath] mutableCopy];
            [dataDic  setObject:[NSString  stringWithFormat:@"%@",[resultDic  objectForKey:@"grade"]] forKey:@"grade"];
            [dataDic  setObject:[NSString  stringWithFormat:@"%@",[resultDic  objectForKey:@"headUrl"]] forKey:@"headUrl"];
            [dataDic  setObject:[NSString  stringWithFormat:@"%@",[resultDic  objectForKey:@"name"]] forKey:@"stuName"];
            [dataDic  setObject:[NSString  stringWithFormat:@"%@",[resultDic  objectForKey:@"stuId"]] forKey:@"stuId"];
            [dataDic  setObject:[NSString  stringWithFormat:@"%@",[resultDic  objectForKey:@"stuNumber"]] forKey:@"stuNum"];
            [dataDic  writeToFile:homePath atomically:YES];
            
            [[ZPAuthStore sharedZPAuthStore] resetUserInfoDictionary];
            [[ZPAuthStore sharedZPAuthStore] setWebViewCookiesWithType:1];
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"logOnNotification" object:nil];
            
        }else{

        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}

//////////////////////////////////////////////////////////////////////////////
@end
